package utils

import (
	"Infinichat_mixed/entity"
	"fmt"
)

func createTable() {
	tables := []interface{}{
		&entity.User{},
		&entity.Friend{},
		&entity.P2PMsg{},
		&entity.P2MMsg{},
		&entity.Group{},
		&entity.GroupMsg{},
		&entity.GroupInvite{},
	}

	for _, table := range tables {
		if !DB.HasTable(table) {
			if err := DB.AutoMigrate(table); err != nil {
				fmt.Printf("Error creating table %T: %v\n", table, err)
			} else {
				fmt.Printf("Table %T created successfully.\n", table)
			}
		} else {
			fmt.Printf("Table %T already exists.\n", table)
			// 如果表已经存在，检查字段变化并进行迁移
			if err := DB.AutoMigrate(table).Error; err != nil {
				fmt.Printf("Error migrating table %T: %v\n", table, err)
			} else {
				fmt.Printf("Table %T migrated successfully.\n", table)
			}
		}
	}
}
